<?php

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * Class User
 * @package app\model
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property int $isAdmin
 * @property string $name
 * @property string $mobile
 * @property string $email
 * @property int $roleId
 * @property string $roleType
 * @property int $status
 * @property string $secretKey
 * @property int $created
 * @property int $updated
 * @property int $systemId
 * @property int $departmentId
 * @property string $token
 * @property User $shortUser
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class User extends \app\common\model\BaseModel
{
    use SoftDelete;

    protected $name = 'User';
    protected $deleteTime = 'deleted';

    public static function signVerifierHash(string $password, string $salt)
    {
        return md5(md5($password) . $salt);
    }

    // 员工
    public function personnels()
    {
        return $this->hasMany(UserPersonnel::class, 'companyId', 'id');
    }

    // 已绑定律师
    public function lawyers()
    {
        return $this->hasMany(Member::class, 'companyId', 'id');
    }

    /**
     * 余额账户
     * @return \think\model\relation\morphOne
     */
    public function account()
    {
        return $this->morphOne(AccountUser::class, ['payMethod', 'id'], PAYMENT_ACCOUNT);
    }

    /**
     * 获客卡账户
     * @return \think\model\relation\morphOne
     */
    public function card()
    {
        return $this->morphOne(AccountUser::class, ['payMethod', 'id'], PAYMENT_CARD);
    }

    /**
     * 通信币账户
     * @return \think\model\relation\morphOne
     */
    public function coin()
    {
        return $this->morphOne(AccountUser::class, ['payMethod', 'id'], PAYMENT_COIN);
    }

    /**
     * 简短的用户信息
     * @param Member $query
     */
    public function scopeShortUser($query)
    {
        $query->tableField(['id', 'name'], $this->getTable());
    }

    // 线下转账
    public function transactionOffline()
    {
        return $this->hasMany(Transaction::class, 'companyId', 'id');
    }

    // 获客卡转账
    public function accountCard()
    {
        return $this->hasOne(AccountUser::class, 'id', 'id');

    }

    // 通信币转账
    public function accountCoin()
    {
        return $this->hasOne(AccountUser::class, 'id', 'id');
    }

    /**
     * 1对1
     * @return \think\model\relation\HasOne
     */
    //    public function one(){
    //        return $this->hasOne( Test1::class, 'Test1表中的键', 'User表中的键');
    //    }

    /**
     * 1对多
     * @return \think\model\relation\HasMany
     */
    //    public function many(){
    //        return $this->hasMany( Test1::class, 'Test1表中的键', 'User表中的键');
    //    }

    /**
     * 多对多
     * @return \think\model\relation\BelongsToMany
     */
    //    public function belongs(){
    //        return $this->belongsToMany( Test1::class, '中间表', '中间表中Test1的键', '中间表中User的键');
    //    }
}
